﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class DispatchDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IDispatchDetailDA dispatchDetailDA = DataAccess.CreateDispatchDetailDA();
        private PubParasInfo pubParasInfo;

        public DispatchDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public DispatchDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public DispatchDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> dispatchDetailInfoIDs, List<string> lineIDs, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (dispatchDetailInfoIDs.Count != lineIDs.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F8200_003", this.pubParasInfo.UserLanguage);
                }
                List<DispatchDetailInfo> dispatchDetailInfos = new List<DispatchDetailInfo>();
                DispatchDetailInfo dispatchDetailInfo = null;
                for (i = 0; i < dispatchDetailInfoIDs.Count; i++)
                {
                    dispatchDetailInfo = dispatchDetailDA.GetDispatchDetailByID(dataBase, tran, dispatchDetailInfoIDs[i], lineIDs[i], this.dataBaseID[0]);
                    dispatchDetailInfos.Add(dispatchDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (dispatchDetailDA.DeleteDispatchDetail(dataBase, tran, dispatchDetailInfoIDs, lineIDs, this.dataBaseID) > 0)
                {
                    for (i = 0; i < dispatchDetailInfoIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<DispatchDetailInfo>(dataBase, tran, dispatchDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "DISPATCH_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + dispatchDetailInfoIDs[i] + " , " + lineIDs[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return dispatchDetailDA.GetAllDispatchDetail(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            DispatchDetailQueryEntity dispatchDetailQuery = obj as DispatchDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(dispatchDetailQuery.DispatchID))
            {
                sqlWhere.Add(" DISPATCH_ID " + dispatchDetailQuery.DispatchID);
            }
            if (!string.IsNullOrEmpty(dispatchDetailQuery.LineID))
            {
                sqlWhere.Add(" LINE_ID " + dispatchDetailQuery.LineID);
            }
            if (!string.IsNullOrEmpty(dispatchDetailQuery.DeliveryOrderID))
            {
                sqlWhere.Add(" DELIVERY_ORDER_ID " + dispatchDetailQuery.DeliveryOrderID);
            }
            if (!string.IsNullOrEmpty(dispatchDetailQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + dispatchDetailQuery.IsActive);
            }
            return dispatchDetailDA.GetDispatchDetailByQueryList(sqlWhere, dispatchDetailQuery, this.dataBaseID[0]);
        }

        public DispatchDetailInfo GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return dispatchDetailDA.GetDispatchDetailByID(mainID, subID, this.dataBaseID[0]);
        }

        public List<DispatchDetailInfo> GetDetailInfoListByID(string mainID, string subID)
        {
            return dispatchDetailDA.GetDispatchDetailListByID(mainID, this.dataBaseID[0]);
        }

        public string InsertDetail(object obj)
        {
            DispatchDetailInfo dispatchDetailInfo = obj as DispatchDetailInfo;
            string dispatchDetailInfoID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (dispatchDetailDA.CheckDispatchDetailIDUnique(dataBase, tran, dispatchDetailInfo.DispatchID, dispatchDetailInfo.LineID.ToString(), this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F8100_005", this.pubParasInfo.UserLanguage, new List<string> { dispatchDetailInfo.DispatchID, dispatchDetailInfo.LineID.ToString() });
                }
                if (dispatchDetailInfo == null)
                {
                    return dispatchDetailInfoID;
                }
                dispatchDetailInfo.UpdatedBy = dispatchDetailInfo.CreatedBy;
                dispatchDetailInfo.CreatedDate = DateTime.Now;
                dispatchDetailInfo.UpdatedDate = DateTime.Now;
                if (dispatchDetailDA.InsertDispatchDetail(dataBase, tran, dispatchDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<DispatchDetailInfo>(dataBase, tran, null, dispatchDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "DISPATCH_DETAIL", this.dataBaseID[0]);
                    dispatchDetailInfoID = dispatchDetailInfo.DispatchID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return dispatchDetailInfoID;
        }

        public string UpdateDetail(object obj)
        {
            DispatchDetailInfo dispatchDetailInfo = obj as DispatchDetailInfo;
            string dispatchDetailInfoID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                dispatchDetailInfo.UpdatedBy = dispatchDetailInfo.CreatedBy;
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                DispatchDetailInfo oldDispatchDetailInfo = dispatchDetailDA.GetDispatchDetailByID(dataBase, tran, dispatchDetailInfo.DispatchID, dispatchDetailInfo.LineID.ToString(), this.dataBaseID[0]);
                if (dispatchDetailDA.UpdateDispatchDetail(dataBase, tran, dispatchDetailInfo, this.dataBaseID) > 0)
                {
                    auditDataBLL.InsertAuditData<DispatchDetailInfo>(dataBase, tran, oldDispatchDetailInfo, dispatchDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "DISPATCH_DETAIL", this.dataBaseID[0]);
                    dispatchDetailInfoID = "DispatchID: " + dispatchDetailInfo.DispatchID + ",LineID:" + dispatchDetailInfo.LineID.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return dispatchDetailInfoID;
        }
    }
}

